package myky.com.doctorapp.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import myky.com.doctorapp.R;
import myky.com.doctorapp.adapter.ViewPagerAdapter;
import myky.com.doctorapp.base.BaseFragment;
import myky.com.doctorapp.base.CMApplication;
import myky.com.doctorapp.entity.PageEntity;
import myky.com.doctorapp.utils.ToastUtils;
import myky.com.doctorapp.wight.CustomViewPager;

/**
 * @author Mr.li
 */
public class RemoteFragment extends BaseFragment {

    public RemoteFragment() {
    }

    @BindView(R.id.fragment_long_range_xiaoxi_iv)
    ImageView mFragmentLongRangeXiaoxiIv;
    @BindView(R.id.fragment_long_range_xiaoxi)
    LinearLayout mFragmentLongRangeXiaoxi;
    @BindView(R.id.titleLayout2)
    LinearLayout mTitleLayout;

    @BindView(R.id.fragment_long_range_tv)
    TextView mFragmentLongRangeTv;
    @BindView(R.id.fragment_long_range_lin)
    LinearLayout mFragmentLongRangeLin;
    @BindView(R.id.fragment_long_range_relative)
    RelativeLayout mFragmentLongRangeRelative;
    @BindView(R.id.fragment_long_range_indicator)
    MagicIndicator mFragmentLongRangeIndicator;
    @BindView(R.id.fragment_long_range_viewpager)
    CustomViewPager mFragmentLongRangeViewpager;
    @BindView(R.id.fragment_long_range_uplaod_remote)
    TextView mFragmentLongRangeUplaodRemote;

    private List<Fragment> mList;
    private FragmentManager manager;
    private ViewPagerAdapter adapter;
    private Activity mainActivity;
    private List<String> strings = Arrays.asList("全部", "待接诊", "已接诊", "已问诊", "已开方", "已取消");
    private List<String> list = Arrays.asList("", "1", "6", "7", "8", "2");


    public RemoteFragment(Activity mainActivity) {
        this.mainActivity = mainActivity;
    }

    public static RemoteFragment newInstance(Activity mainActivity) {
        Bundle args = new Bundle();
        RemoteFragment fragment = new RemoteFragment(mainActivity);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int injectTarget() {
        return 0;
    }

    @Override
    protected void doRetry() {

    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_remote;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).titleBar(mTitleLayout).init();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        setLongRangeList();
    }

    @Override
    public void showDataError(String errorMessage) {

    }

    @Override
    public void showDataSuccess(Object datas) {

    }

    @Override
    public void showPages(PageEntity pageEntity) {

    }


    public void setLongRangeList() {
        if (CMApplication.getInstance().isLoginOk()) {
            mFragmentLongRangeRelative.setVisibility(View.GONE);
            setIndicator();
        } else {
            mFragmentLongRangeRelative.setVisibility(View.VISIBLE);
        }
    }

    //设置指示器
    private void setIndicator() {
        mFragmentLongRangeIndicator.setBackgroundColor(getResources().getColor(R.color.white));
        final CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {

                return strings != null ? strings.size() : 0;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(strings.get(index));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                simplePagerTitleView.setNormalColor(Color.parseColor("#343434"));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.theme_app));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mFragmentLongRangeViewpager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.theme_app));
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setXOffset(-40);
                return indicator;
            }
        });

        mFragmentLongRangeIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mFragmentLongRangeIndicator, mFragmentLongRangeViewpager);

        mList = new ArrayList<>();
        for (int i = 0; i < strings.size(); i++) {
            LongRangeListFragment listFragment = new LongRangeListFragment(list.get(i));
            mList.add(listFragment);
        }
        manager = getChildFragmentManager();
        mFragmentLongRangeViewpager.setOffscreenPageLimit(7);
        adapter = new ViewPagerAdapter(manager, mList);
        mFragmentLongRangeViewpager.setAdapter(adapter);
        mFragmentLongRangeViewpager.setCurrentItem(0);
        mFragmentLongRangeViewpager.setScanScroll(false);
    }

    @OnClick({R.id.fragment_long_range_tv, R.id.fragment_long_range_xiaoxi_iv, R.id.fragment_long_range_uplaod_remote})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fragment_long_range_tv:
                if (CMApplication.getInstance().isLoginOk()) {
                    ToastUtils.showAtCenter("跳转远程门诊");
                } else {

                }
                break;
            case R.id.fragment_long_range_xiaoxi_iv:
                if (CMApplication.getInstance().isLoginOk()) {
                    ToastUtils.showAtCenter("消息页面");
                } else {

                }
                break;
            case R.id.fragment_long_range_uplaod_remote:
                ToastUtils.showAtCenter("跳转远程门诊");
                break;
            default:
        }
    }
}